///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Model/Mesh.h>
#include <Material/Material.h>
#include <Model/MeshManager.h>
#include <Scene/Culling/AABBox.h>
#include <Scene/Culling/Sphere.h>
#include <SettingsManager.h>
#include <Scene/Object.h>
#include <Profiling/ProfilingManager.h>

//-----------------------------------------------------------------------------

Mesh::Mesh( std::string name )
{
	m_material = NULL;
	m_meshData = NULL;

	m_meshName = name;

	m_obb = NULL;
	m_sphere = NULL;

	//MeshManager::GetInstance()->AddMesh( this );
}

//-----------------------------------------------------------------------------

Mesh::~Mesh()
{
	//TODO m_meshData->dereference to know if it's needed and to clean up
	//TODO m_material->dereference to know if it's needed and to clean up
}

//-----------------------------------------------------------------------------

void	Mesh::SetMeshData( MeshData* meshData )
{
	m_meshData = meshData;
}

//-----------------------------------------------------------------------------

MeshData*	Mesh::GetMeshData()
{
	return m_meshData;
}

//-----------------------------------------------------------------------------

void	Mesh::draw( Object* obj ) 
{
	MGL_PROFILE("Mesh::draw");

	//bool	depthRender = SettingsManager::GetInstance()->GetFlag(MGLS_RENDER_DEPTH);

	ASSERT( m_material );
	//if( !depthRender )
	{
		m_material->Activate( obj );
	}


	if( m_meshData )
	{
		m_meshData->Draw();
	}

#if defined(DEBUG)
	if( m_sphere )
	{
		//m_sphere->draw();
	}
#endif

	//if( !depthRender )
	{
		m_material->Deactivate();
	}
}

//-----------------------------------------------------------------------------

bool	Mesh::HasBoundingElement()
{
	return ( m_obb != NULL || m_sphere != NULL );
}

//-----------------------------------------------------------------------------

void	Mesh::generateOBB()
{
	if( !m_obb && m_meshData )
	{
		m_obb = m_meshData->GetOBB();
	}
}

//-----------------------------------------------------------------------------

AABBox*		Mesh::GetOBB()
{
	return m_obb;
}

//-----------------------------------------------------------------------------

void	Mesh::generateBoundingSphere()
{
	if( !m_sphere && m_meshData )
	{
		m_sphere = m_meshData->GetBoundingSphere();
	}
}

//-----------------------------------------------------------------------------

Sphere*		Mesh::GetBoundingSphere()
{
	return m_sphere;
}

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
